package com.sos.core.cards 
{
	import com.sos.utils.ClassUtils;

	public class CardType
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------	
		
		// JSON data property definition
		protected static const 	JSON_PROPERTY_CARD_TYPE:String			= "type";

		// Enum values
		public static const 	PLAYER:CardType							= new CardType(1, "PLAYER");
		public static const 	ACTION:CardType							= new CardType(2, "ACTION");
		public static const 	TRAINING:CardType						= new CardType(3, "TRAINING");
		public static const 	GOALKEEPER:CardType						= new CardType(4, "GOALKEEPER");
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<CardType>;

		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<CardType>();
			_list.push(CardType.PLAYER);
			_list.push(CardType.ACTION);
			_list.push(CardType.TRAINING);
			_list.push(CardType.GOALKEEPER);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function CardType(id:uint, name:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("CardType enum already created!");
			// Init
			_id = id;
			_name = name;
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------	

		public function get id():uint
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * 
		 * @return 
		 */
		public static function names():Array 
		{
			var list:Array  = new Array ;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array  = new Array ;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
		
		/**
		 * Search a CardType by given id 
		 * @param id Id of the card type to find
		 * @return CardType constant for the given id, null if no CardType
		 * for the given id has been found
		 */		
		public static function getById(id:uint):CardType
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("CardType for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a PlayerRole by given name 
		 * @param name Name of the player role to find
		 * @return PlayerRole constant for the given id, null if no PlayerRole
		 * for the given id has been found
		 */		
		public static function getByName(name:String):CardType
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("CardType for name '" + name + "' unknown!");
		}
		
		/**
		 * Search a PlayerRole by given name 
		 * @param name Name of the player role to find
		 * @return PlayerRole constant for the given id, null if no PlayerRole
		 * for the given id has been found
		 */
		public static function getByObject(obj:Object):CardType
		{
			// Check if the object contains the field to retrieve card type id
			if (!obj.hasOwnProperty(JSON_PROPERTY_CARD_TYPE)) throw new Error("Data not valid! Data object doesn't contains '" + JSON_PROPERTY_CARD_TYPE + "' field!");
			// retrieve CardType id
			var id:uint = uint(obj[JSON_PROPERTY_CARD_TYPE]);
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("CardType for id '" + id + "' unknown!");
		}
		
	}
}